// 轮播图
; (function () {
  class GapSwiper {
    constructor(el) {
      this.el = el;
      this.oImg = this.el.find(".banner_img");
      this.oControl = this.el.find(".banner_control");
      this.oLeft = this.el.find(".banner_control .con_left");
      this.oRight = this.el.find(".banner_control .con_right");
      this.oOl = this.el.find("ol");
      this.imgIndex = 0;

      this.autoPlay();
      this.mouseHandler();
      this.clickHandler();
    }
    autoPlay() {
      this.timer = setInterval(() => {
        this.imgIndex++;
        this.autoToggle();
      }, 1500);
    }
    autoToggle() {
      if (this.imgIndex > 2) {
        this.imgIndex = 0;
      }
      this.oImg.children().eq(this.imgIndex).addClass("appear").siblings().removeClass("appear");
      this.oOl.children().eq(this.imgIndex).addClass("active").siblings().removeClass("active");
    }
    mouseHandler() {
      this.el.mouseenter(() => {
        clearInterval(this.timer);
        this.oControl.css("display", "block");
      }).mouseleave(() => {
        this.oControl.css("display", "none");
        this.autoPlay();
      })
    }
    clickHandler() {
      let self = this;
      this.oOl.on("click", "li", function () {
        var $index = $(this).index();
        self.imgIndex = $index;
        self.autoToggle();
      })
      this.oLeft.click(function () {
        console.log(self.oLeft);
        self.imgIndex--;
        if (self.imgIndex < 0) {
          self.imgIndex = 2;
        }
        console.log(self.imgIndex);
        self.oImg.children().eq(self.imgIndex).addClass("appear").siblings().removeClass("appear");
        self.autoToggle();
      })
      this.oRight.click(function () {
        self.imgIndex++;
        if (self.imgIndex > 2) {
          self.imgIndex = 0;
        }
        self.oImg.children().eq(self.imgIndex).addClass("appear").siblings().removeClass("appear");
        self.autoToggle();
      })
    }
  }
  $.fn.extend({
    swiperAuto() {
      new GapSwiper(this);
    }
  })
})();
$(function () {
  $(".bannerWrap").swiperAuto();
})

let userInfo = JSON.parse(sessionStorage.getItem("userinfo") || '[]');
if (userInfo != undefined) {
  $.ajax({
    url: "/user/login",
    type: "post",
    data: userInfo
  }).then(function () {
    console.log(userInfo);
    if (userInfo.length == 0) {
      $(".nav2").find("li a").eq(0).text("你好，请登录");
      $(".nav2").find("li a").eq(1).text("注册");
    } else {
      $(".nav2").find("li a").eq(0).text(userInfo.phone);
      $(".nav2").find("li a").eq(1).text("退出").on("click", () => {
        sessionStorage.clear();
        $(".nav2").find("li a").eq(0).text("你好，请登录");
        $(".nav2").find("li a").eq(1).text("注册");
      });
    }
  });
}
